package com.springboot.news.controller;

import com.springboot.news.domain.dto.UserDTO;
import com.springboot.news.domain.po.Result;
import com.springboot.news.domain.po.User;
import com.springboot.news.service.UserService;
import com.springboot.news.utils.JwtUtil;
import com.springboot.news.utils.ThreadLocalUtil;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api")
@Validated
public class UserController {
    @Resource
    UserService userService;
    @PostMapping("/user/register")
    public Result<String> register(@RequestBody UserDTO dto) {
        String username = dto.getUsername();
        String password = dto.getPassword();
        // 查询用户
//        注册
        User user = userService.findByUserName(username);
        if (user == null) {
            userService.register(username, password);
            return Result.success("注册成功");
        } else {
            return Result.error("用户名重复");
        }
    }
    @PostMapping({"/user/login"})
    public Result<String> login(String username, String password) {
//        System.out.println(username);
        User loginUser = userService.findByUserName(username);
        if (loginUser == null) {
            return Result.error("用户不存在");
        }
        if (password.equals(loginUser.getPassword())) {
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", loginUser.getId());

            claims.put("username", loginUser.getUsername());
            String token = JwtUtil.genToken(claims);
            return Result.success(token);
        }
        return Result.error("密码错误");
    }
    @GetMapping("/user/userInfo")
    public Result<User> userInfo() {
        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        User user = userService.findByUserName(username);
        return Result.success(user);
    }

    @PutMapping("/user/update")
    public Result update(@RequestBody @Validated User user) {
//        Validated 注解做参数校验
        userService.update(user);
        return Result.success();
    }
    @PatchMapping("/user/updateAvatar")
    public Result updateAvatar(@RequestParam String avatarUrl) {
        userService.updateAvatar(avatarUrl);
        return Result.success();
    }
    @PatchMapping("/user/resetPassword")
    public Result resetPassword(@RequestParam Map<String, String> params) {
        System.out.println(params);
        String oldPassword = params.get("old_password");
        String newPassword = params.get("new_password");
        String rePassword = params.get("re_password");
        System.out.println(oldPassword);
        if (!StringUtils.hasLength(oldPassword) || !StringUtils.hasLength(newPassword) || !StringUtils.hasLength(rePassword)) {
            return Result.error("缺少参数");
        }
        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        User user = userService.findByUserName(username);
        if(!user.getPassword().equals(oldPassword)) {
            return Result.error("原密码不对");
        }
        if (!newPassword.equals(rePassword)) {
            return Result.error("新旧密码不匹配");
        }
        userService.resetPassword(newPassword);
        return Result.success();
    }
}
